<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/9 0009
 * Time: 15:47
 */
namespace app\common\controller;

require_once ALI_PATH . "/alipay/AopClient.php";
require_once ALI_PATH . "/alipay/request/AlipayFundTransToaccountTransferRequest.php";

/**
 * 支付宝封装方法
 * Class alipayController
 */
class alipayController
{
    /**
     * 支付宝提现方法
     * @param $orderNumber  订单号
     * @param $money        金额数
     * @param $identity     说明
     * @return array
     * @throws Exception
     * @throws \Exception
     */
    public function payOut($orderNumber,$money,$identity,$name)
    {
        //这里使用的config 是手机端使用的config
        $config = require_once ALI_PATH . "aliconfig.php";

        $aop = new \AopClient();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = $config["app_id"];
        $aop->rsaPrivateKey = $config["rsa_private_key"];// 请填写开发者私钥去头去尾去回车，一行字符串
        $aop->alipayrsaPublicKey = $config["ali_public_key"];//请填写支付宝公钥，一行字符串
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'UTF-8';
        $aop->format = 'json';

        $request = new \AlipayFundTransToaccountTransferRequest();
        $request->setBizContent("{" .
            "\"out_biz_no\":\"$orderNumber\"," .   //商户转账唯一订单号
            "\"payee_type\":\"ALIPAY_LOGONID\"," . //ALIPAY_LOGONID：支付宝登录号
            "\"payee_account\":\"$identity\"," .  //收款方账户。与payee_type配合使用。付款方和收款方不能是同一个账户
            "\"amount\":\"$money\"," .                //转账金额，单位：元。
            "\"payer_show_name\":\"欧贸万享集-提现\"," . //付款方姓名（最长支持100个英文/50个汉字）
            "\"payer_show_name\":\"$name\"," . //收款方真实姓名（最长支持100个英文/50个汉字）
            "\"remark\":\"欧贸万享集-提现\"" .             //转账备注
            "}");
        $result = $aop->execute($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(empty($resultCode) || $resultCode != 10000){
            return ["code"=>500,"msg"=>$result->$responseNode->sub_msg];
        }else {
            return ["code"=>200,"msg"=>"调用成功"];
        }
    }

    /**
     * 支付宝提现方法查询
     * @param $orderNumber
     * @return array
     * @throws \Exception
     */
    public function payOutQuery($orderNumber){
        //这里使用的config 是手机端使用的config
        $config = require_once ALI_PATH . "aliconfig.php";
        $aop = new \AopClient();

        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = $config["app_id"];
        $aop->rsaPrivateKey = $config["rsa_private_key"];// 请填写开发者私钥去头去尾去回车，一行字符串
        $aop->alipayrsaPublicKey=$config["ali_public_key"];//请填写支付宝公钥，一行字符串
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='GBK';
        $aop->format='json';
        $request = new \AlipayFundTransCommonQueryRequest();
        $request->setBizContent("{\"out_biz_no\":\"".$orderNumber."\"}");
        $result = $aop->execute ( $request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;

        dump($result);
        dump($resultCode);
        if(!empty($resultCode)&&$resultCode == 10000){
            return ["code"=>200,"msg"=>"调用成功"];
        } else {
            return ["code"=>500,"msg"=>$result->$responseNode->sub_msg];
        }
    }
}